<template>
  <div class="user-info">
    <!-- 个人信息 -->
    <el-form ref="form" :rules="rules" :model="userInfo" label-width="220px">
      <!--手机 -->

      <el-form-item label="手机" prop="mobile">
        <el-input v-model="userInfo.mobile" style="width:220px" />
      </el-form-item>

      <!-- 工号 入职时间 -->

      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="userInfo.timeOfEntry"
          type="date"
          class="inputW"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>

      <!-- 员工照片 -->

      <el-form-item label="员工头像">
        <!-- 放置上传图片 -->
        <UploadImg ref="haha" />

      </el-form-item>

      <!-- 保存个人信息 -->
      <el-form-item>
        <el-button type="primary" @click="hsave">保存更新</el-button>
        <el-button @click="$router.back()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { saveUserDetailById } from '@/api/employees.js'
import { validMobile } from '@/utils/validate'
import { getUserDetailById } from '@/api/user.js'

export default {

  name: 'Gerenxiangqing',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validMobile(value)) {
        callback(new Error('手机号格式不正确'))
      } else {
        callback()
      }
    }
    return {
      userId: this.$route.query.id,
      userInfo: {
        mobile: '',
        timeOfEntry: ''
      },
      rules: {
        mobile: [{ required: true, trigger: 'blur', validator: validateUsername }]
      }
    }
  },
  created() {
    this.getuser()
  },
  methods: {
    // 获取信息
    async getuser() {
      const res = await getUserDetailById(this.userId)
      console.log(res)
      this.userInfo = res.data
      this.$refs.haha.imageUrl = this.userInfo.staffPhoto
    },
    hsave() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          this.userInfo.staffPhoto = this.$refs.haha.imageUrl
          await saveUserDetailById(this.userInfo)
          this.$message.success('修改成功')
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
  .user-info{
    padding-top:20px;
  }
</style>
